//
//  ZNDIYRefreshHeader.m
//  AiShangCity
//
//  Created by 李振宁 on 15/8/4.
//  Copyright (c) 2015年 LiZhenning. All rights reserved.
//

#import "ZNDIYRefreshHeader.h"
#import "RTSpinKitView.h"

#define HexRGBA(rgbValue, a)     [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]

#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]

@interface ZNDIYRefreshHeader ()

@property (weak, nonatomic) RTSpinKitView *loading;

@property (weak, nonatomic) UILabel *lbTitle;

@end

@implementation ZNDIYRefreshHeader

- (void)prepare
{
    [super prepare];
    
    self.mj_h = 40;
    
    RTSpinKitView *loading = [[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleArc color:HexRGBA(0x4f5660, 1) spinnerSize:30];
    [self addSubview:loading];
    self.loading = loading;
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextColor:RGBACOLOR(104, 104, 104, 1)];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setText:@"下拉更新..."];
    [label setFont:[UIFont systemFontOfSize:15]];
    [self addSubview:label];
    
    self.lbTitle = label;
    
    
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
    self.loading.center = CGPointMake(self.mj_w * 0.5, self.mj_h * 0.5);
    [self.lbTitle setFrame:CGRectMake(0, 0, self.mj_w, self.mj_h)];
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
            
            
        case MJRefreshStateIdle:
            
            
            
            if (oldState == MJRefreshStateRefreshing)
            {
                [self.lbTitle setText:@"更新成功..."];
            }else
            {
                [self.lbTitle setText:@"下拉更新..."];
            }
            
            [self.lbTitle setAlpha:1.0];
            
            [self.loading stopAnimating];
            
            break;
        case MJRefreshStatePulling:
            
            [self.lbTitle setAlpha:1.0];
            [self.lbTitle setText:@"松手更新..."];
            [self.loading stopAnimating];
            
            break;
        case MJRefreshStateRefreshing:
            
            [self.lbTitle setAlpha:0.0];
            [self.lbTitle setText:@"更新中..."];
            
            [self.loading startAnimating];
            
            
            break;
            
        case MJRefreshStateNoMoreData:
            
            [self.lbTitle setAlpha:1.0];
            [self.lbTitle setText:@"更新成功..."];
            [self.loading stopAnimating];
            
            break;
            
        default:
            break;
    }
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    
    if (self.state == MJRefreshStateIdle)
    {
        [self.lbTitle setText:@"下拉更新..."];
    }
    
}


@end
